<?
if($check_me_authed == "OK"){
	
	$today_date				= date("d-m-Y");
	
	$srcDbSequence      = "MST_SEQUENCE";
	$srcDbHdr			      = "PCH_ORDER_HDR";
  $srcDbHdrLog	      = "LOG_PCH_ORDER_HDR";
  $srcDbDtl           = "PCH_ORDER_DTL";
  $srcDbSubDtl        = "PCH_ORDER_SUB_DTL";
    
	if($_POST["selbox"] != ''){
		
		$UID     = unserialize($_SESSION["serUID"]);
    $userId  = $UID->getUID();
    
    $COracle = new oracleDb;
    $COracle->init($_SESSION["ORCL"], 
                   $_SESSION["OPASS"],
                   $_SESSION["SID"],
                   _NO_COMMENT);
    
    if($COracle->connect()){
			
			if(sizeof($_POST["selbox"]) == 0){
				
				usrDie("Error: Nothing to approve");
			}else{
				
				foreach($_POST["selbox"] as $apprThisJobId){
					
					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE 
      		*
      		*/
      		fetchLstDataHeader($COracle,
                             $apprThisJobId,
                             $srcDbHdr,
                             $nHeader,
                             $lstHeader);
					
				  if($lstHeader["STATUS"][0] == _NOT_ACTIVE){
				    			      
			      /**
			      * Build up the REFERENCE number
			      *
			      */
			      $thisPeriode    = &$_SESSION["sysPeriode"];
			      
			      $seqEditStatus  = false;
            
            $thisSequence   = "PO_REF_SEQ";
            
          	while($seqEditStatus == false){
          	  
            	fetchCurSequence($COracle,
              	               $thisSequence,
              	               $thisPeriode,
              	               $srcDbSequence,
              	               $srcJidIndex);
            	
            	initCurSequence($COracle,
              	              $thisSequence,
              	              $srcJidIndex,
              	              $thisPeriode,
              	              $srcDbSequence,
              	              $seqEditStatus);
            }
            
            /**
            * Extract the system period
            *
            */
            $tmpDate = explode("-", $thisPeriode);
            $wrkDate = $tmpDate[0]. /* This period month */
                       "-".
                       $tmpDate[1]; /* This period year */
            
            $refId  = addDigit((string) $srcJidIndex). 
                      $srcJidIndex. 
                      "/" . 
                      $wrkDate;
                      
            procApproval($COracle,
                         $apprThisJobId,
                         $refId,
                         $userId,
                         $srcDbHdr);
                         
            /**
            * Insert Log
            *
            */
            insToDbLog($COracle,
                       $today_date,
                       $apprThisJobId,
                       $userId,
                       $_SERVER["REMOTE_ADDR"],
                       _APPROVE_MODE,
                       $srcDbHdrLog);    

				  }else{
				    
				    usrDie("Error: Tx is ACTIVE/APPROVED/CANCELED");
				  }
					
				}
					
				$COracle->commit();
				
				if($COracle->getStatus() == true){
  
      	  usrMsg("Approval Complete");
      	}else{
      	  
      	  usrMsg("Approval Process Interupted");
      	}
      	
      	$COracle->dc();
				
			}
		}else{
			
			usrDie("Error: oracle connection failed");
		}
	}else{
		
		usrDie("Error: Unknown selection or bad ID retrieved");
		//jumpTo(2,"../".$_SESSION["source_caller"]);
	}
}else{
	
	usrDie("NOT AUTHED");
}
?>